package com.office.automate.servlet.employee;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.frontier.utility.DataConversionUtility;
import com.office.automate.model.authenticate.UserAuthenticator;
import com.office.automate.model.employee.EmployeeAttandanceCommit;
import com.office.automate.service.def.employee.EmployeeService;
import com.office.automate.service.impl.employee.EmployeeServiceImplementer;

/**
 * Servlet implementation class EmployeeAttendanceServlet
 */
@WebServlet("/EmployeeAttendanceServlet")
public class EmployeeAttendanceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DataConversionUtility dcUtility = new DataConversionUtility();

    /**
     * @see HttpServlet#HttpServlet()
     */
    public EmployeeAttendanceServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/employee/EmployeeAttendance.jsp");
		try {
			EmployeeService employeeService = new EmployeeServiceImplementer();
			UserAuthenticator currentUser = (UserAuthenticator) (request.getSession().getAttribute("currentSessionUser"));
			EmployeeAttandanceCommit employeeAttandanceCommit = new EmployeeAttandanceCommit();
			employeeAttandanceCommit.setEmployeeId(currentUser.getEmployeeId());
			employeeAttandanceCommit.setAttandanceDate(dcUtility.convertToDate(request.getParameter("attdate")));
			employeeAttandanceCommit.setSwappingLevel(request.getParameter("level"));
			employeeService.commitAttandanceDetails(employeeAttandanceCommit);
			dispatcher.forward(request, response);	
		} catch (FrontierVisibleException e) {
			request.setAttribute("OperationalError", e);
			dispatcher.forward(request, response);
		}
	}
}
